<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\HttpController\adminapi\setting;

use App\HttpController\adminapi\AuthController;
use App\Model\Admin\SystemRoleModel;
use App\Services\System\Admin\SystemRoleServices;
use App\Services\System\SystemMenusServices;
use EasySwoole\Http\Message\Status;
use EasySwoole\Validate\Validate;

/**
 * Description of SystemRole
 *
 * @author zike
 */
class SystemRole extends AuthController {

    //put your code here


    protected function getValidateRule(?string $action): ?Validate {
	switch ($action) {
	    case 'login':
		$validate = new Validate();
//                $validate->addColumn('imgcode')->required();
//                $validate->addColumn('account')->required()->alphaDash();
//                $validate->addColumn('pwd')->required();
		return $validate;
		break;
	}
	return null;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index() {
	$where = [
	    'status' => $this->request()->getRequestParam('status') ?? '',
	    'role_name' => $this->request()->getRequestParam('role_name') ?? ''
	];
	$ret = SystemRoleServices::getInstance()->getRoleList($where, $this->adminInfo->level + 1);
	return $this->writeJson(Status::CODE_OK, $ret, 'ok');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create() {
	$menus = SystemMenusServices::getInstance()->getMenus($this->adminInfo->level == 0 ? [] : $this->adminInfo->roles);
	return $this->writeJson(Status::CODE_OK, compact('menus'), 'ok');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit() {
	$id = intval($this->request()->getRequestParam('id'));
	if (!$id || !($role = SystemRoleModel::create()->get($id)))
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '数据不存在');
	$menus = SystemMenusServices::getInstance()->getMenus($this->adminInfo->level == 0 ? [] : $this->adminInfo->roles);
	return $this->writeJson(Status::CODE_OK, ['role' => $role->toArray(), 'menus' => $menus], 'ok');
    }

    /**
     * 保存新建的资源
     *
     * @return \think\Response
     */
    public function save() {
	$data = [
	    'role_name' => $this->request()->getRequestParam('role_name') ?? '',
	    'status' => $this->request()->getRequestParam('status') ?? 0,
	    'rules' => $this->request()->getRequestParam('checked_menus') ?? [],
	];

	if (!$data['role_name'])
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '请输入身份名称');
	if (!is_array($data['rules']) || !count($data['rules']))
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '请选择最少一个权限');
	$data['rules'] = implode(',', $data['rules']);
	if ($id = intval($this->request()->getRequestParam('id'))) {
	    if (!($role = SystemRoleModel::create()->get($id)))
		return $this->writeJson(Status::CODE_BAD_REQUEST, [], '数据不存在');
	    if (!$role->update($data))
		return $this->writeJson(Status::CODE_BAD_REQUEST, [], '修改失败');
//	    \think\facade\Cache::clear();//todo
	    return $this->writeJson(Status::CODE_OK, [], '修改成功');
	} else {
	    $data['level'] = $this->adminInfo['level'] + 1;
	    if (!SystemRoleModel::create()->data($data, false)->save())
		return $this->writeJson(Status::CODE_BAD_REQUEST, [], '添加身份失败');
//	    \think\facade\Cache::clear();//todo
	    return $this->writeJson(Status::CODE_OK, [], '添加身份成功');
	}
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete() {
	$id = intval($this->request()->getRequestParam('id'));
	if (!$id) {
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '参数错误，请重新打开');
	}

	if (!SystemRoleModel::create()->destroy($id)) {
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '删除失败,请稍候再试!');
	} else {
	    return $this->writeJson(Status::CODE_OK, [], '删除成功');
	}
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_status() {
	$id = intval($this->request()->getRequestParam('id'));
	if (!$id || !($role = SystemRoleModel::create()->get($id)))
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '数据不存在');

	$status = intval($this->request()->getRequestParam('status'));
	if ($role->update(['status' => $status]))
	    return $this->writeJson(Status::CODE_OK, [], '修改成功');
	else
	    return $this->writeJson(Status::CODE_BAD_REQUEST, [], '修改失败');
    }

}
